var locationDetailInfoDialog;
var showLocationDetailInfo;

// 跳转到车辆信息页面
var goCarInfo;
// 跳转到线路信息页面
var goLineInfo;
// 跳转到驾驶员信息页面
var godrvAnaDriverInfo;

require(['grid', 'topBar', 'layout', 'date'], function () {
    $('#top').topBar({
        left: [
            {
                type: 'filter-more',
                dateType: 'custom',
                search: function () {
                    search();
                },
                item: [
                    {
                        text: '时间快捷',
                        name1: 'beginTime',
                        name2: 'endTime',
                        type: 'dateLine'
                    }, {
                        text: '区域类型',
                        dict: 'LIAREA00',
                        name: 'areaType',
                        type: 'item-select',
                        data: [
                            {text: '全部', value: ''}
                        ]
                    }, {
                        text: '不良行为',
                        url: "/lineDrvStandard/getBadBhvList.htm",
                        name: 'badBhvId',
                        type: 'item-select',
                        key: 'badBhvId',
                        value: 'badBhvName',
                        data: [
                            {text: '全部', value: ''}
                        ]
                    }
                ]
            }
        ],
        right: [
            {
                text: '导出',
                type: 'export-btn',
                authority: '',
                item: [
                    {
                        text: 'Excel导出',
                        click: function () {
                            exportFile('excel');
                        }
                    },
                    {
                        text: 'PDF导出',
                        click: function () {
                            exportFile('pdf');
                        }
                    }
                ]
            }
        ]
    });

    $('body').layout();

    $('#grid').grid({
        url: "/lineDrvStandard/getAreaBadBhvDetailPage.htm",
        multiselect: false,
        colModel: [
            {name: "lineId", label: "lineId", hidden: true},
            {name: "carId", label: "carId", hidden: true},
            {name: "drvId", label: "drvId", hidden: true},
            {name: "beginLoLgt", label: "beginLoLgt", hidden: true},
            {name: "beginLoLtt", label: "beginLoLtt", hidden: true},
            {name: "beginLoDrc", label: "beginLoDrc", hidden: true},
            {name: "badBhvEndTime", label: "badBhvEndTime", hidden: true},
            {
                name: "drvName", label: "驾驶员", width: 55,
                formatter: 'customCurrency',
                formatoptions: {
                    redbtn: true,
                    css: 'text-link c-blue',
                    clickFn: function (rowObject, cellvalue) {
                        godrvAnaDriverInfo(rowObject.drvId);
                    }
                }
            },
            {
                name: "licPltNo", label: "车牌号", width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    var content = "<b><a href='javascript:void(0);' onclick='goCarInfo(\"" + rowObject.carId + "\")' class='text-link c-blue'>" + cellvalue + "</a></b>";
                    return content;
                }
            },
            {name: "carInCd", label: "车辆编号", width: 80},
            {
                name: "lineName", label: "营运线路", width: 80,
                formatter: 'customCurrency',
                formatoptions: {
                    redbtn: true,
                    css: 'text-link c-blue',
                    clickFn: function (rowObject, cellvalue) {
                        goLineInfo(rowObject.lineId);
                    }
                }
            },
            {name: "organName", label: "所属机构", width: 80},
            {name: "lineTy", label: "方向", width: 40},
            {name: "areaType", label: "区域类型", width: 70},
            {
                name: "areaName", label: "发生区域", width: 105,
                formatter: function (cellvalue, options, rowObject) {
                    var areaName;
                    if (rowObject.areaName == null | rowObject.areaName == "" | typeof(rowObject.areaName) == "undefined") {
                        areaName = "未知地址";
                    } else {
                        areaName = rowObject.areaName;
                    }
                    var licPltNo = rowObject.licPltNo || "";
                    var drvName = rowObject.drvName || "";
                    var beginLoLgt = rowObject.beginLoLgt;
                    var beginLoLtt = rowObject.beginLoLtt;
                    var beginLoDrc = rowObject.beginLoDrc;
                    var drvSctBhv = rowObject.badBhvName;
                    var sctBhvBeginTime = rowObject.badBhvBeginTime;
                    var sctBhvEndTime = rowObject.badBhvEndTime;
                    var corpId = rowObject.corpId;
                    var lineId = rowObject.lineId;
                    var content = "<b><a href='javascript:void(0);' onclick='showLocationDetailInfo(\"" +
                        licPltNo + "\",\"" +
                        drvName + "\",\"" +
                        beginLoLgt + "\",\"" +
                        beginLoLtt + "\",\"" +
                        beginLoDrc + "\",\"" +
                        drvSctBhv + "\",\"" +
                        sctBhvBeginTime + "\",\"" +
                        sctBhvEndTime + "\",\"" +
                        corpId + "\",\"" +
                        lineId + "\")' class='text-link c-blue'>" + cellvalue + "</a></b>";
                    return content;
                }
            },
            {name: "badBhvName", label: "异常行为", width: 80},
            {name: "duration", label: "持续时长", width: 80},
            {name: "canSpeed", label: "车速(km/h)", width: 80},
            {name: "rpm", label: "转速(r/min)", width: 80},
            {name: "badBhvBeginTime", label: "发生时间", width: 130}
        ]
    });

    function search() {
        var $postData = $("#top").parent().getValues();
        // 左侧树tab选中的类型
        var type = commonFun.getLeftTreeCurrentTreeType();
        var orgIds = commonFun.getLeftTreeCheckedNodes(type);
        if (!orgIds) {
            $.messager.alert("提示", "请选择查询线路/车辆/驾驶员！", "info");
        } else {
            $postData.type = type;
            $postData.orgIds = orgIds.toString();

            console.log($postData);

            $('#grid').grid('setGridParam', {
                datatype: "json",
                postData: $postData
            }).trigger("reloadGrid");
        }
    }

    function exportFile(fileTy) {
        if ($('#grid').getGridParam("reccount") > 0) {
            var url = "/lineDrvStandard/exportAreaBadBhvDetail.htm";
            var type = commonFun.getLeftTreeCurrentTreeType();
            var orgIds = commonFun.getLeftTreeCheckedNodes(type);
            if (!orgIds) {
                $.messager.alert("提示", "请选择查询线路/车辆/驾驶员！", "info");
            } else {
                var $postData = $("#top").parent().getValues();
                $postData.fileTy = fileTy;
                $postData.type = type;
                $postData.orgIds = orgIds;
                $.exportFile(url, $postData);
            }
        } else {
            $.messager.alert("提示", "请先查询数据！", "info");
        }
    }

    //显示 详细位置信息
    showLocationDetailInfo = function (licPltNo, drvName, beginLoLgt, beginLoLtt, beginLoDrc, type, sctBhvBeginTime, sctBhvEndTime, corpId, lineId) {
        var data = new Array();  // 在这里构造车辆数据的数组
        var obj = new Object();  //车辆数据
        obj.licPltNo = licPltNo; //车牌号
        obj.driverName = drvName; //驾驶员
        obj.loLgt = beginLoLgt; //经度
        obj.loLtt = beginLoLtt; //纬度
        obj.direction = beginLoDrc; //方向
        obj.type = type; //异常类型 字符串表示
        obj.beginTime = sctBhvBeginTime; //开始时间
        obj.endTime = sctBhvEndTime; //结束时间
        obj.corpId = corpId; //开始时间
        obj.lineId = lineId; //结束时间
        data.push(obj);
        openLocationDetailInfoDialog("位置信息", data); //弹出的标题和数据
    };

    function openLocationDetailInfoDialog(title, data) {
        if (!locationDetailInfoDialog) {
            locationDetailInfoDialog = $('<div>').dialog({
                title: title,
                url: './common/map/mapLocation.htm',
                height: $(window.parent).height() * 0.8,
                width: $(window.parent).width() * 0.85
            });
        }
        locationDetailInfoDialog.dialog('open');
        locationDetailInfoDialog.dialog('exec', 'showMapLocation', data);
    }

    //跳转到驾驶员信息页面
    godrvAnaDriverInfo = function (drvId) {
        var data = {};
        data.drvId = drvId;
        $.openTab('8252b8b5e5ff49c08b02f6865562b506', 'showDrvInfo', [data]);
    };
    //跳转到车辆信息页面
    goCarInfo = function (carId) {
        var data = {};
        data.carId = carId;
        $.openTab('6bad880fef164ec6adaea7b18420eb6e', 'showCarInfo', [data]);
    };
    //跳转到线路信息页面
    goLineInfo = function (lineId) {
        var data = {};
        data.lineId = lineId;
        $.openTab('4db17f8a322b4749a61eb2f8a12023ae', 'showLineInfo', [data]);
    };

});